//
//  XLMessageCell.m
//  industry
//
//  Created by xl on 2020/11/26.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLMessageCell.h"
#import "XLMessageModel.h"
#import "XLDateTool.h"

@interface XLMessageCell ()

@property (nonatomic, strong) XLView *bgView;
@property (nonatomic, strong) XLImageView *iconIG;
@property (nonatomic, strong) XLLabel *titleLB;
@property (nonatomic, strong) XLLabel *timeLB;
@property (nonatomic, strong) XLLabel *contentLB;
@property (nonatomic, strong) XLLabel *infoLB;
@property (nonatomic, strong) XLView *redView;


@end

@implementation XLMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor whiteBgColor];
        [self.contentView addSubview:self.bgView];
        [self.bgView addSubview:self.iconIG];
        [_iconIG addSubview:self.redView];
        [self.bgView addSubview:self.titleLB];
        [self.bgView addSubview:self.timeLB];
        [self.bgView addSubview:self.contentLB];
        [self.bgView addSubview:self.infoLB];
        
        [self makeConstraint];
        [self setSelectedBackgroundView:[UIView new]];
    }
    return self;
}

- (void)makeConstraint {
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
    }];
    
    [_iconIG mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(20);
        make.top.equalTo(self.contentView).offset(20);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];

    [_redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.equalTo(self.iconIG);
        make.size.mas_equalTo(CGSizeMake(5, 5));
    }];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIG.mas_right).offset(10);
        make.centerY.equalTo(self.iconIG);
    }];
    
    [_timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconIG);
        make.right.equalTo(self.bgView).offset(-20);
        make.left.greaterThanOrEqualTo(self.titleLB.mas_right).offset(10);
    }];
    
    [_contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(16);
        make.right.equalTo(self.bgView).offset(- 16);
        make.top.equalTo(self.iconIG.mas_bottom).offset(16);
        make.bottom.equalTo(self.infoLB.mas_top).offset(-10);
    }];
    
    [_infoLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(20);
        make.bottom.equalTo(self.contentView).offset(-14);
    }];
}

#pragma mark - Public
- (void)loadCellData:(XLMessageModel *)model isRead:(BOOL)isRead{
    _titleLB.text = model.title;
    _contentLB.text = model.body;
    _redView.hidden = isRead ? YES:NO;
    _timeLB.text = [XLDateTool getDateWithTimeStamp:model.gmtCreated formatter:@"yyyy.MM.dd"];
}

#pragma mark - setter/getter
- (XLView *)bgView {
    if (!_bgView) {
        _bgView = [XLView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 20.0f;
        _bgView.layer.shadowColor = [UIColor colorWithHex:0x000000 alpha:0.08].CGColor;
        _bgView.layer.shadowOffset = CGSizeMake(0,6);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.shadowRadius = 20;
    }
    return _bgView;
}

- (XLImageView *)iconIG {
    if (!_iconIG) {
        _iconIG = [XLImageView new];
        _iconIG.image = XLIMAGE(@"xttz");
    }
    return _iconIG;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:16];
        _titleLB.textColor = [UIColor blackFontColor];
    }
    return _titleLB;
}

- (XLLabel *)timeLB {
    if (!_timeLB) {
        _timeLB = [XLLabel new];
        _timeLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _timeLB.textColor = [UIColor grayFourFontColor];
    }
    return _timeLB;
}

- (XLLabel *)contentLB {
    if (!_contentLB) {
        _contentLB = [XLLabel new];
        _contentLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _contentLB.textColor = [UIColor grayFontColor];
        _contentLB.numberOfLines = 0;
    }
    return _contentLB;
}

- (XLLabel *)infoLB {
    if (!_infoLB) {
        _infoLB = [XLLabel new];
        _infoLB.font = [UIFont fontOfPingFangSCRegularWithSize:12];
        _infoLB.textColor = [UIColor blueThirdFontColor];
        _infoLB.text = @"前往处理>>";
    }
    return _infoLB;
}

- (XLView *)redView {
    if (!_redView) {
        _redView = [XLView new];
        _redView.backgroundColor = [UIColor redColor];
        _redView.layer.cornerRadius = 2.5f;
        _redView.layer.masksToBounds = YES;
    }
    return _redView;
}

@end
